--SELECT * FROM FN_WIP_FLUSH_SHIPPABLE_DETAILS( 11,5985,1012,'ASSEMBLY','1012,1014,1016,1018',1018,2006,1)
ALTER FUNCTION FN_WIP_FLUSH_SHIPPABLE_DETAILS( @LOT_ID INT, @LOT_QTY INT, @SEARCH_PS_ID INT, @SEARCH_PS_TYPE VARCHAR(50), @PATH  NVARCHAR(1000), @LAST_PS_ID INT, @CURR_YEAR INT, @CURRENT_QTR INT )
RETURNS @RESULTS_FINAL_TABLE TABLE ( LOT_ID INT, LOT_NUMBER VARCHAR(50), PROCESSSTEP_ID INT, LOCATION_ID INT, QTY_IN INT, DATE_IN DATETIME, FG_PROCESSSTEP_ID INT, FG_QTY INT, FG_DATE DATETIME )
AS
BEGIN
	DECLARE @ALL_PROCESS_FLOW_CURSOR CURSOR
	DECLARE @BINS_CURSOR CURSOR

	DECLARE @PROCESSSTEP_ID INT
	DECLARE @NEXT_PROCESSSTEP_ID INT

	DECLARE @PS_TYPE_NAME VARCHAR(50)
	DECLARE @BIN_NUMBER VARCHAR(10)
	DECLARE @BIN_VALUE NUMERIC(10,4)

	--DECLARE @LOT_ID INT
	DECLARE @LOT_NUMBER VARCHAR(50)
	DECLARE @LOT_PS_ID INT
	--DECLARE @LOT_QTY INT
	DECLARE @PROCESS_LOT_QTY INT
	DECLARE @DATE_IN DATETIME
	DECLARE @GROSS_DIE INT
	DECLARE @BIN_QTY INT
	DECLARE @SORT_BIN_PERCENTAGE NUMERIC(10,4)
	DECLARE @STD_YIELD NUMERIC(10,4)

	--DECLARE @FAB_PROCESSSTEP_ID INT
	DECLARE @SORT_PROCESSSTEP_ID INT
	DECLARE @DB_PROCESSSTEP_ID INT

	DECLARE @IS_WIP_FLUSH_SORT_BINS_DEFINED INT
	DECLARE @IS_WIP_FLUSH_ASSM_BINS_DEFINED INT
	DECLARE @IS_PROCESS_STARTED BIT
		
	SET @IS_PROCESS_STARTED = 1
	SET @IS_WIP_FLUSH_SORT_BINS_DEFINED = 0
	SET @IS_WIP_FLUSH_ASSM_BINS_DEFINED = 0	

	SELECT @LOT_NUMBER = LOT_NUMBER, @LOT_PS_ID = PROCESSSTEP_ID FROM LOT_TABLE WHERE LOT_ID = @LOT_ID 
	--SELECT TOP 1 @FAB_PROCESSSTEP_ID = PROCESSSTEP_ID FROM FN_GET_PROCESS_FLOW_BY_PROCESSSTEP_ID ( @LOT_PS_ID ) WHERE PROCESS_TYPE_NAME = 'FAB'

	DECLARE @PROCESS_WISE_BINS_TABLE TABLE ( PROCESSSTEP_ID INT, BIN_NUMBER VARCHAR(10), BIN_PERCENTAGE NUMERIC(10,4), RESULT_QTY INT )

	--**********************
	SET @PROCESS_LOT_QTY = @LOT_QTY 
	--**********************

	SELECT TOP 1 @SORT_PROCESSSTEP_ID = PROCESSSTEP_ID FROM FN_GET_PROCESS_FLOW_FROM_PATHSTRING ( @PATH ) WHERE PS_TYPE_NAME = 'SORT'
	SELECT TOP 1 @DB_PROCESSSTEP_ID = PROCESSSTEP_ID FROM FN_GET_PROCESS_FLOW_FROM_PATHSTRING ( @PATH ) WHERE PS_TYPE_NAME = 'DIE BANK'

	INSERT INTO @RESULTS_FINAL_TABLE (LOT_ID, LOT_NUMBER, PROCESSSTEP_ID, LOCATION_ID, QTY_IN, FG_PROCESSSTEP_ID)
	VALUES ( @LOT_ID, @LOT_NUMBER, @LOT_PS_ID, 12, @PROCESS_LOT_QTY, @LAST_PS_ID )

	SET @ALL_PROCESS_FLOW_CURSOR = CURSOR FOR SELECT PROCESSSTEP_ID, PS_TYPE_NAME FROM FN_GET_PROCESS_FLOW_FROM_PATHSTRING ( @PATH ) -- ORDER BY PROCESSSTEP_ID

	OPEN @ALL_PROCESS_FLOW_CURSOR
	FETCH NEXT FROM @ALL_PROCESS_FLOW_CURSOR INTO @PROCESSSTEP_ID, @PS_TYPE_NAME
	WHILE @@FETCH_STATUS = 0
	BEGIN
		IF( @PS_TYPE_NAME = 'FAB' )
		BEGIN
			SELECT @STD_YIELD = CASE WHEN @CURRENT_QTR = 1 THEN ISNULL ( Q1_YIELD, 100 ) WHEN @CURRENT_QTR = 2 THEN ISNULL ( Q2_YIELD, 100) WHEN @CURRENT_QTR = 3 THEN ISNULL ( Q3_YIELD, 100) WHEN @CURRENT_QTR = 4 THEN ISNULL ( Q4_YIELD, 100) END 
			FROM UPDATE_VIEW_COST_TABLE WHERE PROCESSSTEP_ID = @PROCESSSTEP_ID AND YEAR ( FIN_YEAR ) = @CURR_YEAR
			IF( @STD_YIELD IS NULL OR @STD_YIELD = 0 )
			BEGIN
				SET @STD_YIELD = 100
			END
			IF ( @IS_PROCESS_STARTED = 0 )--IGNORE STARTING PROCESS AND CONSIDER NEXT PROCESS OF THIS LOT SINCE WE HAVE DIRECLY HAVE THE QTY_IN '@LOT_QTY' FOR THIS LOT
			BEGIN
				SELECT @GROSS_DIE = ISNULL( PARAMETER_VALUE, 1 ) FROM PROCESS_PARAMETER PP, FN_GET_PROCESS_FLOW_BY_PROCESSSTEP_ID ( @PROCESSSTEP_ID ) TAB2 WHERE PP.PROCESSSTEP_ID = TAB2.PROCESSSTEP_ID AND PARAMETER_KEY = 'GROSS DIE/WAFER' AND PROCESS_TYPE_NAME = 'FAB'
				SELECT @PROCESS_LOT_QTY = ( IL_WAFER_QTY_IN * @GROSS_DIE ), @DATE_IN = IL_DATE_IN FROM INVENTORY_LOT WHERE LOT_ID = @LOT_ID				
			END
			SET @PROCESS_LOT_QTY = ( @PROCESS_LOT_QTY * @STD_YIELD / 100 )

			SET @IS_PROCESS_STARTED = 0
		END
		ELSE
		IF( @PS_TYPE_NAME = 'WAFER STORE' )
		BEGIN
			/*SELECT @STD_YIELD = ISNULL ( Q4_YIELD, 100 ) FROM UPDATE_VIEW_COST_TABLE WHERE PROCESSSTEP_ID = @PROCESSSTEP_ID AND LOCATION_ID = 1 AND YEAR ( FIN_YEAR ) = @CURR_YEAR
			IF( @STD_YIELD IS NULL OR @STD_YIELD = 0 )
			BEGIN
				SET @STD_YIELD = 100
			END*/
			IF ( @IS_PROCESS_STARTED = 0 )--IGNORE STARTING PROCESS AND CONSIDER NEXT PROCESS OF THIS LOT SINCE WE HAVE DIRECLY HAVE THE QTY_IN '@LOT_QTY' FOR THIS LOT
			BEGIN
				SELECT @GROSS_DIE = ISNULL( PARAMETER_VALUE, 1 ) FROM PROCESS_PARAMETER PP, FN_GET_PROCESS_FLOW_BY_PROCESSSTEP_ID ( @PROCESSSTEP_ID ) TAB2 WHERE PP.PROCESSSTEP_ID = TAB2.PROCESSSTEP_ID AND PARAMETER_KEY = 'GROSS DIE/WAFER' AND PROCESS_TYPE_NAME = 'FAB'
				SELECT @PROCESS_LOT_QTY = ( IL_WAFER_QTY_IN * @GROSS_DIE ), @DATE_IN = IL_DATE_IN FROM INVENTORY_LOT WHERE LOT_ID = @LOT_ID
			END
			--SET @PROCESS_LOT_QTY = ( @PROCESS_LOT_QTY * @STD_YIELD / 100 )
			SET @IS_PROCESS_STARTED = 0
		END
		ELSE IF( @PS_TYPE_NAME = 'SORT' )
		BEGIN
			SELECT @STD_YIELD = CASE WHEN @CURRENT_QTR = 1 THEN ISNULL ( Q1_YIELD, 100 ) WHEN @CURRENT_QTR = 2 THEN ISNULL ( Q2_YIELD, 100) WHEN @CURRENT_QTR = 3 THEN ISNULL ( Q3_YIELD, 100) WHEN @CURRENT_QTR = 4 THEN ISNULL ( Q4_YIELD, 100) END 
			FROM UPDATE_VIEW_COST_TABLE WHERE PROCESSSTEP_ID = @PROCESSSTEP_ID AND YEAR ( FIN_YEAR ) = @CURR_YEAR
			IF( @STD_YIELD IS NULL OR @STD_YIELD = 0 )
			BEGIN
				SET @STD_YIELD = 100
			END
			--IF ( @IS_PROCESS_STARTED = 0 )--IGNORE STARTING PROCESS AND CONSIDER NEXT PROCESS OF THIS LOT SINCE WE HAVE DIRECLY HAVE THE QTY_IN '@LOT_QTY' FOR THIS LOT
			--BEGIN
				SET @BINS_CURSOR = CURSOR FOR SELECT NEXT_PROCESSSTEP_ID, BIN_NUMBER, BIN_VALUE FROM WIP_FLUSH_BIN_SETUP_TABLE WHERE PROCESSSTEP_ID = @PROCESSSTEP_ID

				OPEN @BINS_CURSOR
				FETCH NEXT FROM @BINS_CURSOR INTO @NEXT_PROCESSSTEP_ID, @BIN_NUMBER, @BIN_VALUE
				WHILE @@FETCH_STATUS = 0
				BEGIN
					INSERT INTO @PROCESS_WISE_BINS_TABLE ( PROCESSSTEP_ID, BIN_NUMBER , BIN_PERCENTAGE, RESULT_QTY )
					VALUES ( @PROCESSSTEP_ID, @BIN_NUMBER, @BIN_VALUE, (  ( @PROCESS_LOT_QTY * @STD_YIELD / 100 ) * ( @BIN_VALUE/100 ) ) )

					SET @NEXT_PROCESSSTEP_ID = NULL
					SET @BIN_NUMBER = NULL
					SET @BIN_VALUE = NULL
					SET @IS_WIP_FLUSH_SORT_BINS_DEFINED = ISNULL ( @IS_WIP_FLUSH_SORT_BINS_DEFINED, 0 ) + 1

					FETCH NEXT FROM @BINS_CURSOR INTO @NEXT_PROCESSSTEP_ID, @BIN_NUMBER, @BIN_VALUE
				END
				CLOSE @BINS_CURSOR
				DEALLOCATE @BINS_CURSOR	
			/*END
			ELSE
			BEGIN
				SET @PROCESS_LOT_QTY = ( @PROCESS_LOT_QTY * @STD_YIELD / 100 )
				UPDATE @RESULTS_FINAL_TABLE SET FG_QTY = @PROCESS_LOT_QTY WHERE  FG_PROCESSSTEP_ID = @LAST_PS_ID
				--SET @SEARCH_PS_TYPE = 'DIE BANK'
			END*/
			SET @IS_PROCESS_STARTED = 0			
		END
		ELSE IF( @PS_TYPE_NAME = 'DIE BANK' )
		BEGIN	
			IF ( @SEARCH_PS_TYPE = 'DIE BANK' )
			BEGIN

				--SELECT TOP 1 @SORT_PROCESSSTEP_ID = PROCESSSTEP_ID FROM FN_GET_PROCESS_FLOW_BY_PROCESSSTEP_ID ( @PROCESSSTEP_ID ) WHERE PROCESS_TYPE_NAME = 'SORT'
				IF( @SORT_PROCESSSTEP_ID IS NULL OR @SORT_PROCESSSTEP_ID = 0)
				BEGIN
					SELECT TOP 1 @SORT_PROCESSSTEP_ID = PROCESSSTEP_ID FROM FN_GET_PROCESS_FLOW_BY_PROCESSSTEP_ID ( @PROCESSSTEP_ID ) WHERE PROCESS_TYPE_NAME = 'SORT'
				END
				--###################################
				DELETE FROM @PROCESS_WISE_BINS_TABLE
				--###################################
				INSERT INTO @PROCESS_WISE_BINS_TABLE ( PROCESSSTEP_ID, BIN_NUMBER , RESULT_QTY )
				SELECT @SORT_PROCESSSTEP_ID, 'BIN1', SUM( ISNULL( BIN_1_BALANCE_QTY, ISNULL( BIN_1, 0 ) ) ) FROM SORT_BIN_RELEASE_TABLE SBRT, LOT_TABLE LT WHERE SBRT.LOT_ID = LT.LOT_ID AND ISNULL( BIN_1_BALANCE_QTY, ISNULL(BIN_1,0) ) > 0 AND SBRT.LOT_ID = @LOT_ID
				UNION					
				SELECT @SORT_PROCESSSTEP_ID, 'BIN2', SUM( ISNULL( BIN_2_BALANCE_QTY, ISNULL( BIN_2, 0 ) ) ) FROM SORT_BIN_RELEASE_TABLE SBRT, LOT_TABLE LT WHERE SBRT.LOT_ID = LT.LOT_ID AND ISNULL( BIN_2_BALANCE_QTY, ISNULL(BIN_2,0) ) > 0 AND SBRT.LOT_ID = @LOT_ID
				UNION
				SELECT @SORT_PROCESSSTEP_ID, 'BIN3', SUM( ISNULL( BIN_3_BALANCE_QTY, ISNULL( BIN_3, 0 ) ) ) FROM SORT_BIN_RELEASE_TABLE SBRT, LOT_TABLE LT WHERE SBRT.LOT_ID = LT.LOT_ID AND ISNULL( BIN_3_BALANCE_QTY, ISNULL(BIN_3,0) ) > 0 AND SBRT.LOT_ID = @LOT_ID
				UNION
				SELECT @SORT_PROCESSSTEP_ID, 'BIN4', SUM( ISNULL( BIN_4_BALANCE_QTY, ISNULL( BIN_4, 0 ) ) ) FROM SORT_BIN_RELEASE_TABLE SBRT, LOT_TABLE LT WHERE SBRT.LOT_ID = LT.LOT_ID AND ISNULL( BIN_4_BALANCE_QTY, ISNULL(BIN_4,0) ) > 0 AND SBRT.LOT_ID = @LOT_ID
				UNION
				SELECT @SORT_PROCESSSTEP_ID, 'BIN5', SUM( ISNULL( BIN_5_BALANCE_QTY, ISNULL( BIN_5, 0 ) ) ) FROM SORT_BIN_RELEASE_TABLE SBRT, LOT_TABLE LT WHERE SBRT.LOT_ID = LT.LOT_ID AND ISNULL( BIN_5_BALANCE_QTY, ISNULL(BIN_5,0) ) > 0 AND SBRT.LOT_ID = @LOT_ID
				UNION
				SELECT @SORT_PROCESSSTEP_ID, 'BIN6', SUM( ISNULL( BIN_6_BALANCE_QTY, ISNULL( BIN_6, 0 ) ) ) FROM SORT_BIN_RELEASE_TABLE SBRT, LOT_TABLE LT WHERE SBRT.LOT_ID = LT.LOT_ID AND ISNULL( BIN_6_BALANCE_QTY, ISNULL(BIN_6,0) ) > 0 AND SBRT.LOT_ID = @LOT_ID
				UNION
				SELECT @SORT_PROCESSSTEP_ID, 'BIN7', SUM( ISNULL( BIN_7_BALANCE_QTY, ISNULL( BIN_7, 0 ) ) ) FROM SORT_BIN_RELEASE_TABLE SBRT, LOT_TABLE LT WHERE SBRT.LOT_ID = LT.LOT_ID AND ISNULL( BIN_7_BALANCE_QTY, ISNULL(BIN_7,0) ) > 0 AND SBRT.LOT_ID = @LOT_ID
				UNION
				SELECT @SORT_PROCESSSTEP_ID, 'BIN8', SUM( ISNULL( BIN_8_BALANCE_QTY, ISNULL( BIN_8, 0 ) ) ) FROM SORT_BIN_RELEASE_TABLE SBRT, LOT_TABLE LT WHERE SBRT.LOT_ID = LT.LOT_ID AND ISNULL( BIN_8_BALANCE_QTY, ISNULL(BIN_8,0) ) > 0 AND SBRT.LOT_ID = @LOT_ID
				UNION
				SELECT @SORT_PROCESSSTEP_ID, 'BIN9', SUM( ISNULL( BIN_9_BALANCE_QTY, ISNULL( BIN_9, 0 ) ) ) FROM SORT_BIN_RELEASE_TABLE SBRT, LOT_TABLE LT WHERE SBRT.LOT_ID = LT.LOT_ID AND ISNULL( BIN_9_BALANCE_QTY, ISNULL(BIN_9,0) ) > 0 AND SBRT.LOT_ID = @LOT_ID
				UNION
				SELECT @SORT_PROCESSSTEP_ID, 'BIN10', SUM( ISNULL( BIN_10_BALANCE_QTY, ISNULL( BIN_10, 0 ) ) ) FROM SORT_BIN_RELEASE_TABLE SBRT, LOT_TABLE LT WHERE SBRT.LOT_ID = LT.LOT_ID AND ISNULL( BIN_10_BALANCE_QTY, ISNULL(BIN_10,0) ) > 0 AND SBRT.LOT_ID = @LOT_ID

				SET @IS_WIP_FLUSH_SORT_BINS_DEFINED = ISNULL( @IS_WIP_FLUSH_SORT_BINS_DEFINED, 0 ) + 1
			END
			SET @IS_PROCESS_STARTED = 0	
		END
		ELSE IF( @PS_TYPE_NAME = 'ASSEMBLY' )
		BEGIN
			SELECT @STD_YIELD = CASE WHEN @CURRENT_QTR = 1 THEN ISNULL ( Q1_YIELD, 100 ) WHEN @CURRENT_QTR = 2 THEN ISNULL ( Q2_YIELD, 100) WHEN @CURRENT_QTR = 3 THEN ISNULL ( Q3_YIELD, 100) WHEN @CURRENT_QTR = 4 THEN ISNULL ( Q4_YIELD, 100) END 
			FROM UPDATE_VIEW_COST_TABLE WHERE PROCESSSTEP_ID = @PROCESSSTEP_ID AND YEAR ( FIN_YEAR ) = @CURR_YEAR
			IF( @STD_YIELD IS NULL OR @STD_YIELD = 0 )
			BEGIN
				SET @STD_YIELD = 100
			END
			IF ( @IS_PROCESS_STARTED = 0 )--IGNORE STARTING PROCESS AND CONSIDER NEXT PROCESS OF THIS LOT SINCE WE HAVE DIRECLY HAVE THE QTY_IN '@LOT_QTY' FOR THIS LOT
			BEGIN
				--SELECT TOP 1 @SORT_PROCESSSTEP_ID = PROCESSSTEP_ID FROM FN_GET_PROCESS_FLOW_BY_PROCESSSTEP_ID ( @PROCESSSTEP_ID ) WHERE PROCESS_TYPE_NAME = 'SORT'
				IF( @SORT_PROCESSSTEP_ID IS NULL OR @SORT_PROCESSSTEP_ID = 0)
				BEGIN
					SELECT TOP 1 @SORT_PROCESSSTEP_ID = PROCESSSTEP_ID FROM FN_GET_PROCESS_FLOW_BY_PROCESSSTEP_ID ( @PROCESSSTEP_ID ) WHERE PROCESS_TYPE_NAME = 'SORT'
				END
				IF( @DB_PROCESSSTEP_ID IS NULL OR @DB_PROCESSSTEP_ID = 0)
				BEGIN
					SELECT TOP 1 @DB_PROCESSSTEP_ID = PROCESSSTEP_ID FROM FN_GET_PROCESS_FLOW_BY_PROCESSSTEP_ID ( @PROCESSSTEP_ID ) WHERE PROCESS_TYPE_NAME = 'DIE BANK'
				END

				SET @BINS_CURSOR = CURSOR FOR SELECT NEXT_PROCESSSTEP_ID, BIN_NUMBER, BIN_VALUE FROM WIP_FLUSH_BIN_SETUP_TABLE WHERE PROCESSSTEP_ID = @DB_PROCESSSTEP_ID AND NEXT_PROCESSSTEP_ID = @PROCESSSTEP_ID AND BIN_VALUE > 0

				IF( @IS_WIP_FLUSH_SORT_BINS_DEFINED > 0 )
				BEGIN
					OPEN @BINS_CURSOR
					FETCH NEXT FROM @BINS_CURSOR INTO @NEXT_PROCESSSTEP_ID, @BIN_NUMBER, @BIN_VALUE
					WHILE @@FETCH_STATUS = 0
					BEGIN
						IF ( @BIN_VALUE > 0 ) 
						BEGIN
							SELECT @BIN_QTY = RESULT_QTY, @SORT_BIN_PERCENTAGE = ISNULL( BIN_PERCENTAGE, 100) FROM @PROCESS_WISE_BINS_TABLE WHERE PROCESSSTEP_ID = @SORT_PROCESSSTEP_ID AND BIN_NUMBER = @BIN_NUMBER			
	
							INSERT INTO @PROCESS_WISE_BINS_TABLE ( PROCESSSTEP_ID, BIN_NUMBER , BIN_PERCENTAGE, RESULT_QTY )
							VALUES ( @PROCESSSTEP_ID, @BIN_NUMBER, @BIN_VALUE, (  ( @BIN_QTY * @STD_YIELD / 100 ) * ( @BIN_VALUE/100 ) ) )
						
							SET @BIN_QTY = NULL		
							SET @IS_WIP_FLUSH_ASSM_BINS_DEFINED = ISNULL ( @IS_WIP_FLUSH_ASSM_BINS_DEFINED, 0 ) + 1
						END
						SET @BIN_VALUE = NULL
						SET @BIN_NUMBER = NULL
						SET @NEXT_PROCESSSTEP_ID = NULL
						FETCH NEXT FROM @BINS_CURSOR INTO @NEXT_PROCESSSTEP_ID, @BIN_NUMBER, @BIN_VALUE
					END
					CLOSE @BINS_CURSOR
					DEALLOCATE @BINS_CURSOR

					SELECT @BIN_QTY = ISNULL( SUM(RESULT_QTY), 0) FROM @PROCESS_WISE_BINS_TABLE WHERE PROCESSSTEP_ID = @PROCESSSTEP_ID
					SET @PROCESS_LOT_QTY =  @BIN_QTY
					UPDATE @RESULTS_FINAL_TABLE SET  FG_QTY = @BIN_QTY WHERE  FG_PROCESSSTEP_ID = @LAST_PS_ID
				END
				ELSE
				BEGIN
					SET @PROCESS_LOT_QTY =  ( @PROCESS_LOT_QTY * @STD_YIELD / 100 )
					UPDATE @RESULTS_FINAL_TABLE SET  FG_QTY = @PROCESS_LOT_QTY WHERE  FG_PROCESSSTEP_ID = @LAST_PS_ID
				END
			END
			ELSE
			BEGIN
				SET @PROCESS_LOT_QTY =  ( @PROCESS_LOT_QTY * @STD_YIELD / 100 )
				UPDATE @RESULTS_FINAL_TABLE SET  FG_QTY = @PROCESS_LOT_QTY WHERE  FG_PROCESSSTEP_ID = @LAST_PS_ID
			END
			SET @IS_PROCESS_STARTED = 0			

		END
		ELSE IF( @PS_TYPE_NAME = 'TEST' )
		BEGIN	
			SELECT @STD_YIELD = CASE WHEN @CURRENT_QTR = 1 THEN ISNULL ( Q1_YIELD, 100 ) WHEN @CURRENT_QTR = 2 THEN ISNULL ( Q2_YIELD, 100) WHEN @CURRENT_QTR = 3 THEN ISNULL ( Q3_YIELD, 100) WHEN @CURRENT_QTR = 4 THEN ISNULL ( Q4_YIELD, 100) END 
			FROM UPDATE_VIEW_COST_TABLE WHERE PROCESSSTEP_ID = @PROCESSSTEP_ID AND YEAR ( FIN_YEAR ) = @CURR_YEAR

			IF( @STD_YIELD IS NULL OR @STD_YIELD = 0 )
			BEGIN
				SET @STD_YIELD = 100
			END
			--IF ( @IS_PROCESS_STARTED = 0 )
			--BEGIN
				SET @PROCESS_LOT_QTY =  ( @PROCESS_LOT_QTY * @STD_YIELD / 100 )
				UPDATE @RESULTS_FINAL_TABLE SET  FG_QTY = @PROCESS_LOT_QTY WHERE  FG_PROCESSSTEP_ID = @LAST_PS_ID
			--END
			SET @IS_PROCESS_STARTED = 0
		END
		ELSE IF( @PS_TYPE_NAME = 'CLASS STORE' )
		BEGIN	
			/*SELECT @STD_YIELD = ISNULL ( Q4_YIELD, 100 ) FROM UPDATE_VIEW_COST_TABLE WHERE PROCESSSTEP_ID = @PROCESSSTEP_ID AND YEAR ( FIN_YEAR ) = @CURR_YEAR
			IF( @STD_YIELD IS NULL OR @STD_YIELD = 0 )
			BEGIN
				SET @STD_YIELD = 100
			END
			SET @PROCESS_LOT_QTY =  ( @PROCESS_LOT_QTY * @STD_YIELD / 100 )*/
			UPDATE @RESULTS_FINAL_TABLE SET  FG_QTY = @PROCESS_LOT_QTY WHERE  FG_PROCESSSTEP_ID = @LAST_PS_ID

			SET @IS_PROCESS_STARTED = 0
		END
		ELSE IF( @PS_TYPE_NAME = 'MARK & PACK' )
		BEGIN	
			/*SELECT @STD_YIELD = ISNULL ( Q4_YIELD, 100 ) FROM UPDATE_VIEW_COST_TABLE WHERE PROCESSSTEP_ID = @PROCESSSTEP_ID AND YEAR ( FIN_YEAR ) = @CURR_YEAR
			IF( @STD_YIELD IS NULL OR @STD_YIELD = 0 )
			BEGIN
				SET @STD_YIELD = 100
			END
			SET @PROCESS_LOT_QTY =  ( @PROCESS_LOT_QTY * @STD_YIELD / 100 )*/
			UPDATE @RESULTS_FINAL_TABLE SET  FG_QTY = @PROCESS_LOT_QTY WHERE  FG_PROCESSSTEP_ID = @LAST_PS_ID

			SET @IS_PROCESS_STARTED = 0
		END
		ELSE IF( @PS_TYPE_NAME = 'FINISHED GOODS' )
		BEGIN	
			/*SELECT @STD_YIELD = ISNULL ( Q4_YIELD, 100 ) FROM UPDATE_VIEW_COST_TABLE WHERE PROCESSSTEP_ID = @PROCESSSTEP_ID AND YEAR ( FIN_YEAR ) = @CURR_YEAR
			IF( @STD_YIELD IS NULL OR @STD_YIELD = 0 )
			BEGIN
				SET @STD_YIELD = 100
			END
			SET @PROCESS_LOT_QTY =  ( @PROCESS_LOT_QTY * @STD_YIELD / 100 )*/
			UPDATE @RESULTS_FINAL_TABLE SET  FG_QTY = @PROCESS_LOT_QTY WHERE  FG_PROCESSSTEP_ID = @LAST_PS_ID

			SET @IS_PROCESS_STARTED = 0
		END
	
		SET @BIN_QTY = NULL
		SET @SORT_BIN_PERCENTAGE = NULL
		SET @STD_YIELD = NULL
		SET @PS_TYPE_NAME = NULL
		SET @PROCESSSTEP_ID = NULL
				
		FETCH NEXT FROM @ALL_PROCESS_FLOW_CURSOR INTO @PROCESSSTEP_ID, @PS_TYPE_NAME
	END
	CLOSE @ALL_PROCESS_FLOW_CURSOR
	DEALLOCATE @ALL_PROCESS_FLOW_CURSOR

	RETURN
END